//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Xml;
using CommunityServer.Configuration;

namespace CommunityServer.Components
{
	/// <summary>
	/// Summary description for EmailJob.
	/// </summary>
	public class EmailJob : IJob
	{
        private int _failureInterval = 15;
        private int _numberOfTries = 10;

		public EmailJob()
		{
			//
			// TODO: Add constructor logic here
			//
		}

	    public void Execute(XmlNode node)
	    {
			// Get the failureInterval setting
			XmlAttribute failureIntervalNode = node.Attributes["failureInterval"];
			XmlAttribute numberOfTriesNode = node.Attributes["numberOfTries"];

			if(failureIntervalNode != null)
			{
				try { this._failureInterval = int.Parse(failureIntervalNode.Value); }
				catch { this._failureInterval = 15; }
			}

			if(numberOfTriesNode != null)
			{
				try { this._numberOfTries = int.Parse(numberOfTriesNode.Value); }
				catch { this._numberOfTries = 10; }
			}
          
            SiteSettingsManager.IterateSiteSettings(new SiteSettingsListIterator(SendQueuedEmailJob));
	    }

        public void SendQueuedEmailJob(int settingsID)
        {
            CSContext ctx = CSContext.Create(settingsID);
            Emails.SendQueuedEmails(this._failureInterval, this._numberOfTries, ctx.SiteSettings); 
        }
	}
}
